package com.example.demo.controller;

import com.example.demo.model.Book;
import com.example.demo.model.User;

import com.example.demo.service.SearchFeignService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 功能描述：
 *
 * @Author: Zhanghf
 * @Date: 2021/5/28 17:57
 */
@Controller
public class SearchController {
//    @Autowired(required = false)
//    private ElasticsearchService elasticsearchService;
@Autowired(required = false)
private SearchFeignService searchFeignService;
    @Autowired
    private ObjectMapper objectMapper;
    @PostMapping("search")
    public String search(HttpServletResponse response, HttpServletRequest request, Model model, HttpSession session) throws JsonProcessingException {
        String keyWord = request.getParameter("keyWord");
//         List<Book> searchList = bookMapper.selectList(new QueryWrapper<Book>().like("name", keyWord).or().like("author", keyWord));
     //   SearchHits<Book> hits = elasticsearchService.select(keyWord);
//        model.addAttribute("searchList",searchList);
        String json = searchFeignService.searchbymysql(keyWord);
        ArrayList<Book> books = objectMapper.readValue(json, new TypeReference<ArrayList<Book>>() {
        });
        model.addAttribute("searchList",books);
        User user= (User) session.getAttribute("user");
        model.addAttribute("user",user);
        model.addAttribute("keyWord",keyWord);
        return "search";
    }}

